//
//  Status.swift
//  NSinaWeibo
//
//  Created by LoliQueen on 16/2/20.
//  Copyright © 2016年 LoliQueen. All rights reserved.
//

import UIKit

class Status: NSObject {
    
    //微博id
    var id : Int64 = 0
    //微博创建时间
    var created_at : String?
    //微博正文
    var text :String?
    //微博来源
    var source : String?
    
    ///用户模型
    var user : User?
    
    ///配图视图数组
    var pic_urls : [[String : String]]?{
    
        //重写set方法 给pictureUrls初始化
        didSet{
            
            guard let urls  = pic_urls else{
                return
            }
            //如果有图片地址有值，遍历数组，取值
            pictureUrls = [NSURL]()
            for item in urls{

                let urlString = item["thumbnail_pic"]
                ///替换地址获得大的高清图
                let urlStr = urlString?.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                let url = NSURL(string: urlStr!)
                pictureUrls?.append(url!)
            }
            
        }
    }
    
    ///计算型属性，配图视图地址数组
    var pictureUrls : [NSURL]?
    
    //转发微博属性
    var retweeted_status : Status?
    
    
    //kvc赋值
    init(dict: [String : AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    ///给user 初始化
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            //将anyobject转换为字典类型
            if let dict = value as? [String : AnyObject] {
                user = User(dict: dict)
            }
            //需要提前终止，负责super 还会在做一次赋值
            return
        }
        
        if key == "retweeted_status" {
            //将anyobject转换为字典类型
            if let dict = value as? [String : AnyObject] {
                retweeted_status = Status(dict: dict)
            }
            //需要提前终止，负责super 还会在做一次赋值
            return
        }
        ///写在下面 当key == user 时 少执行一步赋值
        super.setValue(value, forKey: key)
        
    }
    
    ///过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    
}
